-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
【Hackathon 5th No.38】为 Paddle 新增 FractionalMaxPool2d / FractionalMaxPool3d API #59130
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
Update 20231128API 应该已经写好了,CI 已经通过 ~ 相较 RFC 有几点改变:
请评审 ~ 谢谢!;) |
forward : max_pool2d_with_index(Tensor x, int[] kernel_size, int[] strides = {1, 1}, int[] paddings = {0, 0}, bool global_pooling = false, bool adaptive = false) -> Tensor(out), Tensor(mask) | ||
args : (Tensor x, Tensor mask, Tensor out_grad, int[] kernel_size, int[] strides, int[] paddings, bool global_pooling, bool adaptive) | ||
forward : max_pool2d_with_index(Tensor x, int[] kernel_size, int[] strides = {1, 1}, int[] paddings = {0, 0}, bool global_pooling = false, bool adaptive = false, bool fractional = false, float random_u = 0.0) -> Tensor(out), Tensor(mask) | ||
args : (Tensor x, Tensor mask, Tensor out_grad, int[] kernel_size, int[] strides, int[] paddings, bool global_pooling, bool adaptive, bool fractional, float random_u) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这儿直接改变原来phi算子定义会有不兼容升级的问题,新增api的话新增一个phi接口,可以复用已有的max_pool2d_with_index
接口即可
forward : max_pool3d_with_index(Tensor x, int[] kernel_size, int[] strides = {1, 1, 1}, int[] paddings = {0, 0, 0}, bool global_pooling = false, bool adaptive = false) -> Tensor(out), Tensor(mask) | ||
args : (Tensor x, Tensor mask, Tensor out_grad, int[] kernel_size, int[] strides, int[] paddings, bool global_pooling, bool adaptive) | ||
forward : max_pool3d_with_index(Tensor x, int[] kernel_size, int[] strides = {1, 1, 1}, int[] paddings = {0, 0, 0}, bool global_pooling = false, bool adaptive = false, bool fractional = false, float random_u = 0.0) -> Tensor(out), Tensor(mask) | ||
args : (Tensor x, Tensor mask, Tensor out_grad, int[] kernel_size, int[] strides, int[] paddings, bool global_pooling, bool adaptive, bool fractional, float random_u) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上
@@ -1687,7 +1687,7 @@ | |||
backward : matrix_power_grad | |||
|
|||
- op : max_pool2d_with_index | |||
args : (Tensor x, int[] kernel_size, int[] strides= {1, 1}, int[] paddings = {0, 0}, bool global_pooling = false, bool adaptive = false) | |||
args : (Tensor x, int[] kernel_size, int[] strides= {1, 1}, int[] paddings = {0, 0}, bool global_pooling = false, bool adaptive = false, bool fractional = false, float random_u = 0.0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上
@@ -1696,7 +1696,7 @@ | |||
backward : max_pool2d_with_index_grad | |||
|
|||
- op : max_pool3d_with_index | |||
args : (Tensor x, int[] kernel_size, int[] strides = {1, 1, 1}, int[] paddings = {0, 0, 0}, bool global_pooling = false, bool adaptive = false) | |||
args : (Tensor x, int[] kernel_size, int[] strides = {1, 1, 1}, int[] paddings = {0, 0, 0}, bool global_pooling = false, bool adaptive = false, bool fractional = false, float random_u = 0.0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上
@@ -658,6 +658,8 @@ void MaxPoolWithIndexGradInferMeta(const MetaTensor& x, | |||
const std::vector<int>& paddings, | |||
bool global_pooling, | |||
bool adaptive, | |||
bool fractional, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
infermeta也需要新增一个复用之前的即可
@@ -2229,6 +2229,8 @@ void MaxPoolWithIndexInferMeta(const MetaTensor& x, | |||
const std::vector<int>& paddings, | |||
bool global_pooling, | |||
bool adaptive, | |||
bool fractional, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上
@@ -335,6 +335,8 @@ void MaxPoolWithIndexInferMeta(const MetaTensor& x, | |||
const std::vector<int>& paddings, | |||
bool global_pooling, | |||
bool adaptive, | |||
bool fractional, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上
@@ -1571,6 +1571,8 @@ class MaxPool2dWithIndexFunctor<CPUContext, T1, T2> { | |||
const std::vector<int>& strides, | |||
const std::vector<int>& paddings, | |||
bool adaptive, | |||
bool fractional, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
直接修改原kernel定义也会有不兼容升级的问题,可以新增一个kernel来复用当前kernel
@@ -150,6 +150,8 @@ void MaxPoolWithIndexGradRawKernel(const Context& ctx, | |||
const std::vector<int>& paddings, | |||
bool global_pooling, | |||
bool adaptive, | |||
bool fractional, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这儿不可以直接修改原有kernel签名,会有不兼容升级的问题,新增的算子可以考虑复用这个算子。
from paddle import base | ||
from paddle.base import core | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为了方便管理,测试最好继承自optest,需要支持低精度fp16, bfp16等,尽可能覆盖全算子类别。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
测试有两部分:
test/legacy_test/test_pool_max_op.py
这里是测试底层算子的单测文件,继承了OpTest
,覆盖了全算子类别 ~test/legacy_test/test_fractional_max_pool2d.py
和test/legacy_test/test_fractional_max_pool3d.py
测试的是 python api ,参考的是test/legacy_test/test_adaptive_max_pool2d.py
和test/legacy_test/test_adaptive_max_pool3d.py
,继承unittest.TestCase
,这里也要继承OpTest
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
测试有两部分:
test/legacy_test/test_pool_max_op.py
这里是测试底层算子的单测文件,继承了OpTest
,覆盖了全算子类别 ~test/legacy_test/test_fractional_max_pool2d.py
和test/legacy_test/test_fractional_max_pool3d.py
测试的是 python api ,参考的是test/legacy_test/test_adaptive_max_pool2d.py
和test/legacy_test/test_adaptive_max_pool3d.py
,继承unittest.TestCase
,这里也要继承OpTest
?
这儿需要在yaml中新增两个算子定义,也就是新增两个op,我们针对单个op的测试都是用OpTest
去统一管理的,所以继承OpTest
是比较合理的测试方式,也便于后续维护。针对API的话,OpTest是可以覆盖到动态图API测试的,check_dygraph
开关就是测试动态图API,静态图API测试可以继承自 unittest.TestCase
。
关于复用
如果不修改目前的 kernel 算子签名,如何复用? 还请帮忙看一下 ~ 非常感谢! 是否可以在 ops.yaml/backward.yaml 等处新增算子描述,然后在 也就是说,是保留原来的算子,但是内部复用现在增加了 |
yaml处需要新增算子描述,新增两个算子,如果想复用原有kernel的话确实可以考虑你说的那种情况,增加一个重载,不要改变原有kernel的参数以及行为就好。 |
PR types
New features
PR changes
APIs
Description
【Hackathon 5th No.38】为 Paddle 新增 FractionalMaxPool2d / FractionalMaxPool3d API
RFC:PaddlePaddle/community#698
涉及文件:
paddle/phi/api/yaml/backward.yaml
反向算子描述paddle/phi/api/yaml/ops.yaml
前向算子描述paddle/phi/infermeta/backward.cc
反向算子paddle/phi/infermeta/backward.h
反向算子paddle/phi/infermeta/unary.cc
算子 InferMetapaddle/phi/infermeta/unary.h
算子 InferMetapaddle/phi/kernels/funcs/pooling.cc
cpu 算子实现paddle/phi/kernels/funcs/pooling.cu
gpu 算子实现paddle/phi/kernels/funcs/pooling.h
算子头文件paddle/phi/kernels/impl/pool_grad_kernel_impl.h
paddle/phi/kernels/impl/pool_kernel_impl.h
paddle/phi/kernels/pool_grad_kernel.h
paddle/phi/kernels/pool_kernel.h
python/paddle/nn/__init__.py
暴露 APIpython/paddle/nn/functional/__init__.py
暴露 APIpython/paddle/nn/functional/pooling.py
API 实现python/paddle/nn/layer/__init__.py
暴露 layer APIpython/paddle/nn/layer/pooling.py
layer API 实现test/legacy_test/test_fractional_max_pool2d.py
2d api/layer 单测test/legacy_test/test_fractional_max_pool3d.py
3d api/layer 单测test/legacy_test/test_pool_max_op.py
算子单测paddle/phi/kernels/xpu/pool_kernel.cc
xpu 算子paddle/phi/kernels/xpu/pool_grad_kernel.cc
xpu 反向请评审 ~